Forums » Users » sel
Select date in calendarSelect date in calendar

Pages: 1
Python code to convert C4d materials to Centileo, How to replace the old materials by there centileo clones using python?
 
sel
User  Posts: 3
Feb 21, 2023 10:13
Hollo Kirgman, thanks for your reply
that's good news to hear ^^
Python code to convert C4d materials to Centileo, How to replace the old materials by there centileo clones using python?
 
sel
User  Posts: 3
Feb 21, 2023 08:53
I found a quick and dirty way to do that.. :)

by adding "return material0" to the CreatesCNTLmat function, and modifying main function like so..
Code
import c4d


# Get C4D material values
def MatConvertGet(material1):
    # Get the material's Name
    matname = material1.GetName()

    # Get the material's shaders
    shaderImg = material1[c4d.MATERIAL_COLOR_SHADER]
    shaderCol = material1[c4d.MATERIAL_COLOR_COLOR]

    shaderBumpImg = material1[c4d.MATERIAL_BUMP_SHADER]
    Bump_stat = material1[c4d.MATERIAL_USE_BUMP]

    shaderBumpVal = material1[c4d.MATERIAL_BUMP_STRENGTH]


    Col_texture_path = ""
    #Get color shader's texture path or color
    if shaderImg:
        Col_texture_path = shaderImg[c4d.BITMAPSHADER_FILENAME]

    Bump_texture_path = ""
    #Get bump shader's texture path or color
    if shaderBumpImg:
        Bump_texture_path = shaderBumpImg[c4d.BITMAPSHADER_FILENAME]


    return {"matname" : matname,
            "shaderCol" : shaderCol,
            "Col_texture_path" : Col_texture_path,
            "Bump_stat" : Bump_stat,
            "shaderBumpVal" : shaderBumpVal,
            "Bump_texture_path" : Bump_texture_path,}



# Create the centileo Clone
def CreatesCNTLmat(matname, shaderCol, Col_texture_path, Bump_stat, shaderBumpVal, Bump_texture_path):
    # Create the centileo Clone
    c4d.CallCommand(58000, 1036659) # creat new CNTL material
    material0 = doc.GetActiveMaterial() # Get the setected new material
    material0.SetName("SNTL_"+matname)

    # Color And Texture
    material0[c4d.IDD_DIFFUSE_COLOR] = shaderCol

    material0[c4d.IDD_REFL1_ROUGH] = 0.6

    if  Col_texture_path:
        Shader1 = c4d.BaseShader(1036812)
        Shader1[c4d.CNTL_XBITMAP_NAME] = "CColor"
        Shader1[c4d.CNTL_XBITMAP_FILENAME] = Col_texture_path
        material0.InsertShader(Shader1)
        material0[c4d.IDD_DIFFUSE_COLOR_X] = Shader1

    # bump map
    material0[c4d.IDD_TOGGLE_BUMP] = Bump_stat


    if Bump_stat:
        material0[c4d.IDD_BUMP] = shaderBumpVal

    if Bump_stat and Bump_texture_path:
        Shader2 = c4d.BaseShader(1036812)
        Shader2[c4d.CNTL_XBITMAP_NAME] = "BBump"
        Shader2[c4d.CNTL_XBITMAP_FILENAME] = Bump_texture_path
        material0.InsertShader(Shader2)
        material0[c4d.IDD_BUMP_X] = Shader2
        
    return material0
        

# Main function
def main():
    # Get the active material
    material1 = doc.GetActiveMaterials()

    for mat in material1:
        # Get C4D material values
        C4D_mat = MatConvertGet(mat)
        
        doc.SetActiveMaterial(mat)
        c4d.CallCommand(16370) # Select Material Tags/Objects
        
        tg = doc.GetActiveTags() # Get the active tags
        
        # Creat the new Centileo material
        newmat = CreatesCNTLmat(C4D_mat["matname"], C4D_mat["shaderCol"], C4D_mat["Col_texture_path"], C4D_mat["Bump_stat"], C4D_mat["shaderBumpVal"], C4D_mat["Bump_texture_path"])
       
        #loop through all the material tags of the old material and replace it with the new material
        for tag in tg:
            tag[c4d.TEXTURETAG_MATERIAL] = newmat
            
        # Delete the old material
        doc.SetActiveMaterial(mat)
        c4d.CallCommand(300001024) 
        
    # Update the document
    c4d.EventAdd()

if __name__=='__main__':
    main()
Edited: sel - Feb 21, 2023 08:57
Python code to convert C4d materials to Centileo, How to replace the old materials by there centileo clones using python?
 
sel
User  Posts: 3
Feb 21, 2023 05:47
Hello,
I made a quick code to convert C4d Materials to centileo for a projects I'm working on..
the code just handels color and bump shaders that what I need for my project.. and its not complete working code beacose it's not handling expectations and errors ..etc.

Code
import c4d


# Get  material's Vals
def MatConvertGet(material1):
    # Get the material's Name
    matname = material1.GetName()

    # Get the material's shaders
    shaderImg = material1[c4d.MATERIAL_COLOR_SHADER]
    shaderCol = material1[c4d.MATERIAL_COLOR_COLOR]

    shaderBumpImg = material1[c4d.MATERIAL_BUMP_SHADER]
    Bump_stat = material1[c4d.MATERIAL_USE_BUMP]

    shaderBumpVal = material1[c4d.MATERIAL_BUMP_STRENGTH]


    Col_texture_path = ""
    #Get the shader's texture path or color
    if shaderImg:
        Col_texture_path = shaderImg[c4d.BITMAPSHADER_FILENAME]

    Bump_texture_path = ""
    #Get the shader's texture path or color
    if shaderBumpImg:
        Bump_texture_path = shaderBumpImg[c4d.BITMAPSHADER_FILENAME]


    return {"matname" : matname,
            "shaderCol" : shaderCol,
            "Col_texture_path" : Col_texture_path,
            "Bump_stat" : Bump_stat,
            "shaderBumpVal" : shaderBumpVal,
            "Bump_texture_path" : Bump_texture_path,}


# Create the centileo Clone
def CreatesCNTLmat(matname, shaderCol, Col_texture_path, Bump_stat, shaderBumpVal, Bump_texture_path):
    # Create the centileo Clone
    c4d.CallCommand(58000, 1036659) # creat new CNTL material
    material0 = doc.GetActiveMaterial() # Get the setected new material
    material0.SetName("SNTL_"+matname)

    # Color And Texture
    material0[c4d.IDD_DIFFUSE_COLOR] = shaderCol

    material0[c4d.IDD_REFL1_ROUGH] = 0.6

    if  Col_texture_path:
        Shader1 = c4d.BaseShader(1036812)
        Shader1[c4d.CNTL_XBITMAP_NAME] = "CColor"
        Shader1[c4d.CNTL_XBITMAP_FILENAME] = Col_texture_path
        material0.InsertShader(Shader1)
        material0[c4d.IDD_DIFFUSE_COLOR_X] = Shader1

    # bump map
    material0[c4d.IDD_TOGGLE_BUMP] = Bump_stat


    if Bump_stat:
        material0[c4d.IDD_BUMP] = shaderBumpVal

    if Bump_stat and Bump_texture_path:
        Shader2 = c4d.BaseShader(1036812)
        Shader2[c4d.CNTL_XBITMAP_NAME] = "BBump"
        Shader2[c4d.CNTL_XBITMAP_FILENAME] = Bump_texture_path
        material0.InsertShader(Shader2)
        material0[c4d.IDD_BUMP_X] = Shader2


# Main function
def main():
    # Get the active material
    material1 = doc.GetActiveMaterials()

    for mat in material1:
        C4D_mat = MatConvertGet(mat)
        CreatesCNTLmat(C4D_mat["matname"], C4D_mat["shaderCol"], C4D_mat["Col_texture_path"], C4D_mat["Bump_stat"], C4D_mat["shaderBumpVal"], C4D_mat["Bump_texture_path"])

    # Update the document
    c4d.EventAdd()

if __name__=='__main__':
    main()
the code just create a centileo clone but not replace the original materials,
so my question is how to replace the old materials by there centileo clones after creating them automatically using python of course


PS: I'm not a programmer by any means, I just know a little of python with some help from chatGPT :)
Edited: sel - Feb 21, 2023 06:45
Pages: 1